package com.taokatao.cardmee.business.cardcrud;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.DigitsKeyListener;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.taokatao.cardmee.BaseActivity;
import com.taokatao.cardmee.R;
import com.taokatao.cardmee.Utils;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class InfoEditActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.v_fit) View vFit;
    @BindView(R.id.t_cancel) TextView tCancel;
    @BindView(R.id.t_submit) TextView tSubmit;
    @BindView(R.id.e_input) EditText eInput;
    @BindView(R.id.t_title) TextView tTitle;

    private String originContent;
    private boolean isNeedChangedContentToSave = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_info_edit);
        ButterKnife.bind(this);

        Utils.fitSysWindowAndSetStatusBarMode(0xFFFFFFFF, vFit);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            tCancel.setBackgroundResource(R.drawable.ripple_back_black);
            tSubmit.setBackgroundResource(R.drawable.ripple_back_black);
        }

        String title = getIntent().getStringExtra("extra_title");
        originContent = getIntent().getStringExtra("extra_content");

        isNeedChangedContentToSave = getIntent().getBooleanExtra("extra_need_changed", true);

        int inputType = getIntent().getIntExtra("extra_input_type", InputType.TYPE_CLASS_TEXT);
        int maxLength = getIntent().getIntExtra("extra_max_length", 0);
        final String inputDigits = getIntent().getStringExtra("extra_digits");

        Utils.logd("InfoEditActivity title[%s] originContent[%s] inputType[%d] maxLength[%d] inputDigits[%s]",
                title, originContent, inputType, maxLength, inputDigits);

        Utils.throttleFirstClick(tSubmit, this);

        tTitle.setText(title);
        eInput.setInputType(inputType | InputType.TYPE_TEXT_FLAG_MULTI_LINE);

        ArrayList<InputFilter> limitRules = new ArrayList<>();
        if (maxLength > 0) {
            InputFilter lengthFilter = new InputFilter.LengthFilter(maxLength);
            limitRules.add(lengthFilter);
        }
        if (limitRules.size() > 0) {
            eInput.setFilters(limitRules.toArray(new InputFilter[limitRules.size()]));
        }

        if (!TextUtils.isEmpty(inputDigits)) {
            eInput.setKeyListener(DigitsKeyListener.getInstance(inputDigits));
        }

        eInput.setText(originContent);
        eInput.setSelection(TextUtils.isEmpty(originContent) ? 0 : originContent.length());

        tSubmit.setEnabled(!isNeedChangedContentToSave);
        tSubmit.setTextColor(isNeedChangedContentToSave ? 0xFFAAAAAA : 0xFF333333);

        eInput.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!TextUtils.equals(originContent, eInput.getText())) {
                    tSubmit.setEnabled(true);
                    tSubmit.setTextColor(0xFF333333);
                } else {
                    tSubmit.setEnabled(false);
                    tSubmit.setTextColor(0xFFAAAAAA);
                }

            }
        });
    }

    @Override
    @OnClick({R.id.t_cancel})
    public void onClick(View v) {
        if (v == tCancel) {
            finish();
        } else if (v == tSubmit) {
            Intent intent = new Intent();
            intent.putExtra("result", eInput.getText().toString());
            setResult(RESULT_OK, intent);
            finish();
        }
    }
}
